package ashbyutils.email;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import junit.framework.TestCase;

/**
 * EmailTest
 * 
 * Insert Class Description/Role here
 * 
 * @author Greg.Ashby
 * @date 17-Aug-2004
 */
public class EmailTest extends TestCase {
   private final static String EMAIL_ADDRESS = "ashbygreg@yahoo.com";
   private final static String SUBJECT = "[Junit Test]";
   private final static String BODY = "Someone has run the EmailTest Test case";
   private final static String HTML_BODY = "<html><head><title>This is the title</title></head><body>This is the body.<b>This should be bold</b></body></html>";

   /**
    * Constructor for EmailTest.
    * 
    * @param arg0
    *  
    */
   public EmailTest(String arg0) {
      super(arg0);
   }

   /**
    * @throws Exception
    */
   /*
    * @see TestCase#setUp()
    */
   protected void setUp() throws Exception {
      super.setUp();
   }

   /**
    * @throws Exception
    */
   /*
    * @see TestCase#tearDown()
    */
   protected void tearDown() throws Exception {
      super.tearDown();
   }

   /**
    * Test Purpose: tests the validation method on an email instance
    * 
    * Test Dialogue: 1) create an email 2) validate it - should throw exception 3) set all necessary fields 4) validate it - should not
    * throw an exception 5) for each necessary field 6) unset it 7) validate it - should throw an exception 8) set it back
    */
   public void testValidate() {

      System.out.println("\n\n********** Starting Validate **********\n");

      Email anEmail = Email.createInstance();
      assertIllegalState(anEmail);
      populateEmail(anEmail);
      anEmail.validate();
      anEmail.setTo(null);
      assertIllegalState(anEmail);
      anEmail.setTo(EMAIL_ADDRESS);
      anEmail.setFrom(null);
      assertIllegalState(anEmail);
      anEmail.setFrom(EMAIL_ADDRESS);
      anEmail.setBody(null);
      assertIllegalState(anEmail);

      System.out.println("\n********** Ending Validate **********\n");

   }

   /**
    * @param anEmail
    */
   private void populateEmail(Email anEmail) {

      anEmail.setTo(EMAIL_ADDRESS);
      anEmail.setFrom(EMAIL_ADDRESS);
      anEmail.setBody(BODY);
      anEmail.setSubject(SUBJECT);
   }

   /**
    * @param anEmail
    */
   private void assertIllegalState(Email anEmail) {
      try {
         anEmail.validate();
         assertTrue(false);
      } catch (IllegalStateException ise) {
      }
   }

   /**
    * Test Purpose: sends a test email to the value of "emailAddress"
    * 
    * Test Dialogue: 1) create an email, populate it, and send it... there's no way to assert it got there so just assume if no
    * exceptions occured it passed... NOTE: you may want to manually verify the email got there.
    * 
    * @throws AddressException
    * @throws MessagingException
    */
   public void testSendEmail() throws AddressException, MessagingException {

      System.out.println("\n\n********** Starting SendEmail **********\n");

      Email anEmail = Email.createInstance();
      populateEmail(anEmail);
      anEmail.setBcc("ashbygreg@gmail.com");
      //anEmail.send();

      System.out.println("\n********** Ending SendEmail **********\n");

   }

   /**
    * Verifies that an email can be sent with only the CC field set and not the TO field
    */
   public void testCCOnly() throws Exception {

      System.out.println("\n\n********** Starting CCOnly **********\n");

      Email anEmail = Email.createInstance();
      populateEmail(anEmail);
      anEmail.setCc(anEmail.getTo());
      anEmail.setTo(null);
      //anEmail.send();

      System.out.println("\n********** Ending CCOnly **********\n");

   }

   /**
    * REQUIREMENTS: -
    */
   public void testHtml() throws Exception {

      System.out.println("\n\n********** Starting Html **********\n");

      Email anEmail = Email.createInstance();
      populateEmail(anEmail);
      anEmail.setHtml(true);
      anEmail.setBody(HTML_BODY);
      //anEmail.send();

      System.out.println("\n********** Ending Html **********\n");

   }
}